#!/usr/local/bin/lua
-- a = 10
-- repeat
--     print("a:", a)
--     a = a + 1
-- until( a > 15)

-- b = 10
-- while(b < 15)
-- do
--     print("B:", b)
--     b = b + 1
-- end

-- function f(x)
--     return x * 5
-- end

-- for i=1,5 do
--     print("I:", i)
-- end

tab = {"b", "b", "c"}
-- print(ipairs(tab))

for i, v in pairs(tab) do
    if (i == 1) then print(v) end
    if (v == 'c') then print(i) end
end
myprint = function(param)
   print("这是打印函数 -   ##",param,"##")
end

function add(num1,num2,functionPrint)
   result = num1 + num2
   -- 调用传递的函数参数
   functionPrint(result)
end
myprint(10)
-- myprint 函数作为参数传递
add(2,5,myprint)
-- 可变参数
function bu( ... )
    print(...)
end

bu(1,2,3,4,5)

fruits = {"banana","orange","apple","grapes"}
print("排序前")
for k,v in ipairs(fruits) do
        print(k,v)
end

table.sort(fruits)
print("排序后")
for k,v in ipairs(fruits) do
        print(k,v)
end

require("mo")

print(mo.add(1, 2))
mo.fun3()

mo.fun1()

mytable = {}
mymetatable = {}
print(mytable)
print(mymetatable)
setmetatable(mytable, mymetatable)
print(getmetatable(mytable))

co = coroutine.create(
    function(i)
        print(i);
    end
)
 
coroutine.resume(co, 1)   -- 1
print(coroutine.status(co))  -- dead
 
print("----------")
 
co = coroutine.wrap(
    function(i)
        print(i);
    end
)
 
co(1)
 
print("----------")
 
co2 = coroutine.create(
    function()
        for i=1,10 do
            print(i)
            if i == 3 then
                print(coroutine.status(co2))  --running
                print(coroutine.running()) --thread:XXXXXX
            end
            coroutine.yield()
        end
    end
)
 
coroutine.resume(co2) --1
coroutine.resume(co2) --2
coroutine.resume(co2) --3
 
print(coroutine.status(co2))   -- suspended
print(coroutine.running())
 
print("----------")


local newProductor

function productor()
     local i = 0
     while true do
          i = i + 1
          print("生成的i:", i)
          send(i)     -- 将生产的物品发送给消费者
     end
end

function consumer()
     while true do
          local i = receive()     -- 从生产者那里得到物品
          print(i)
          if i > 100 then break end
     end
end

function receive()
     local status, value = coroutine.resume(newProductor)
     
     return value
end

function send(x)
     coroutine.yield(x)     -- x表示需要发送的值，值返回以后，就挂起该协同程序
end

-- 启动程序
-- newProductor = coroutine.create(productor)
-- consumer()

print("文件操作")
file = io.open("mo.lua", "r")
io.input(file)

print(io.read())
io.close(file)

file = io.open("mo.lua", "a")
io.output(file)

io.write("\n -- 文件末尾注释")

io.close(file)

luasql = require "luasql.mysql"

--创建环境对象
env = luasql.mysql()

--连接数据库
conn = env:connect("lua","lua","wYfeMhcbdHGpnE8W","101.132.153.82",3306)

--设置数据库的编码格式
conn:execute"SET NAMES UTF8"
print(conn)
--执行数据库操作
cur = conn:execute("select * from role")
print(cur)
row = cur.fetch(cur, {},"a")
print(row)
--文件对象的创建
file = io.open("role.txt","w+");

while row do
    var = string.format("%d %s\n", row.id, row.name)

    print(var)

    file:write(var)

    row = cur:fetch(row,"a")
end


file:close()  --关闭文件对象
conn:close()  --关闭数据库连接
env:close()   --关闭数据库环境